package actions;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import db.dao.Classes;
import db.dao.ClassesDAO;
import db.dao.Dept;
import db.dao.DeptDAO;
import db.dao.Major;
import db.dao.MajorDAO;
import db.dao.Student;
import db.dao.StudentDAO;
/*修改学生信息*/
public class UpdateStudentAction extends ActionSupport{
	private String sid="";	//学生编号 必填
	private String sname="";	//学生姓名
	private String ssex="";	//学生性别
	private String siden="";	//学生身份证号
	private Short slen;	//学制
	private String sclass="";	//所属班级
	private String smajor2="";	//所属专业
	private String sdept2="";	//所属学院
	
	public UpdateStudentAction() {
	}

	public String getSid() {
		return sid;
	}

	public void setSid(String sid) {
		this.sid = sid;
	}

	public String getSname() {
		return sname;
	}

	public void setSname(String sname) {
		this.sname = sname;
	}

	public String getSsex() {
		return ssex;
	}

	public void setSsex(String ssex) {
		this.ssex = ssex;
	}

	public String getSiden() {
		return siden;
	}

	public void setSiden(String siden) {
		this.siden = siden;
	}

	public Short getSlen() {
		return slen;
	}

	public void setSlen(Short slen) {
		this.slen = slen;
	}

	public String getSclass() {
		return sclass;
	}

	public void setSclass(String sclass) {
		this.sclass = sclass;
	}

	public String getSmajor2() {
		return smajor2;
	}

	public void setSmajor2(String smajor2) {
		this.smajor2 = smajor2;
	}

	public String getSdept2() {
		return sdept2;
	}

	public void setSdept2(String sdept2) {
		this.sdept2 = sdept2;
	}
	
	public String execute() throws IOException {
		////获取页面信息用于弹窗提示和清除页面缓存
		HttpServletResponse response = ServletActionContext.getResponse();
		//用于获取结果
		String result="";
		//查找学生
		StudentDAO sDao = new StudentDAO();
		Student student = sDao.findById(sid);
		//判断是否查找到
		if (student!=null) {
			//如果查找到 则对每个输入项判空 不为空则修改
			if (!sclass.equals("")) {
				ClassesDAO cDao = new ClassesDAO();
				Classes classes = cDao.findById(sclass);
				student.setClasses(classes);
			}
			if (!smajor2.equals("")) {
				MajorDAO mDao = new MajorDAO();
				Major major = mDao.findById(smajor2);
				student.setMajor(major);
			}
			if (!sdept2.equals("")) {
				DeptDAO dDao = new DeptDAO();
				Dept dept = dDao.findById(sdept2);
				student.setDept(dept);
			}
			if (slen!=null&&!slen.equals("")) {
				student.setStuedulength(slen);
			}
			if (!siden.equals("")) {
				student.setStuidentify(siden);
			}
			if (!sname.equals("")) {
				student.setStuname(sname);
			}
			if (!ssex.equals("")) {
				student.setStusex(ssex);
			}
			//保存
			result = sDao.save(student);
			if (result.equals("success")) {
				//成功弹窗提示
				response.setContentType("text/html;charset=UTF-8");
	            response.setCharacterEncoding("UTF-8");
	            PrintWriter printWriter = response.getWriter();
	            printWriter.print("<script>alert('修改成功!')</script>");
	            printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
	            printWriter.flush();
	            printWriter.close();
	            response.setHeader("Pragma","No-cache");   
	            response.setHeader("Cache-Control","no-cache");  
	            response.setHeader("Cache-Control", "no-store");  
	            response.setDateHeader("Expires", 0);
				return null;
			}else {
				//失败弹窗提示
				response.setContentType("text/html;charset=UTF-8");
	            response.setCharacterEncoding("UTF-8");
	            PrintWriter printWriter = response.getWriter();
	            printWriter.print("<script>alert('修改失败!')</script>");
	            printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
	            printWriter.flush();
	            printWriter.close();
	            response.setHeader("Pragma","No-cache");   
	            response.setHeader("Cache-Control","no-cache");  
	            response.setHeader("Cache-Control", "no-store");  
	            response.setDateHeader("Expires", 0);
				return null;
			}
		}else {
			//未查询到学生弹窗提示
			response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter printWriter = response.getWriter();
            printWriter.print("<script>alert('未查询到该学生!')</script>");
            printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
            printWriter.flush();
            printWriter.close();
            response.setHeader("Pragma","No-cache");   
            response.setHeader("Cache-Control","no-cache");  
            response.setHeader("Cache-Control", "no-store");  
            response.setDateHeader("Expires", 0);
			return null;
		}
	}
}
